<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改文档')" />
    <th:block th:include="include :: summernote-css" />
    <th:block th:include="include :: select2-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-document-edit" th:object="${docDocument}" enctype="multipart/form-data">
            <input name="documentId" th:field="*{documentId}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">文档分类：</label>
                    <div class="col-sm-8">
                        <select name="categoryId" class="form-control select2-hidden-accessible" id="categoryId" required>
                            <option value="">请选择分类</option>
                            <option th:each="category : ${categories}" th:value="${category.categoryId}" th:text="${category.categoryName}" th:selected="${category.categoryId == docDocument.categoryId}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">文档标题：</label>
                    <div class="col-sm-8">
                        <input name="title" th:field="*{title}" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">当前文件：</label>
                    <div class="col-sm-8">
                        <p class="form-control-static" th:text="*{fileName}"></p>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">上传新文件：</label>
                    <div class="col-sm-8">
                        <input name="file" type="file" accept=".txt,.doc,.docx">
                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 如需替换文件，请上传新文件，否则保留原文件</span>
                    </div>
                </div>
            </div>
            <input name="fileName" th:field="*{fileName}" type="hidden">
            <input name="filePath" th:field="*{filePath}" type="hidden">
            <input name="fileSize" th:field="*{fileSize}" type="hidden">
            <input name="fileType" th:field="*{fileType}" type="hidden">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">文档内容(仅存储txt内容)：</label>
                    <div class="col-sm-8">
                        <input type="hidden" class="form-control" th:field="*{content}">
                        <div class="summernote" id="content"></div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: summernote-js" />
    <th:block th:include="include :: select2-js" />
    <script th:inline="javascript">
        var prefix = ctx + "system/document";
        
        $(function() {
            // 初始化Select2
            $(".select2-hidden-accessible").select2({
                width: "100%"
            });
            
            $("#form-document-edit").validate({
                focusCleanup: true
            });

            function submitHandler() {
                if ($.validate.form()) {
                    var formData = new FormData($("#form-document-edit")[0]);
                    $.ajax({
                        type: "post",
                        url: prefix + "/edit",
                        data: formData,
                        contentType: false,
                        processData: false,
                        success: function(result) {
                            $.operate.successCallback(result);
                        },
                        error: function(xhr, status, error) {
                            $.modal.alertError("更新失败: " + error);
                        }
                    });
                }
            }

            $('.summernote').each(function(i) {
                $('#' + this.id).summernote({
                    lang: 'zh-CN',
                    dialogsInBody: true,
                    callbacks: {
                        onChange: function(contents, $edittable) {
                            $("input[name='" + this.id + "']").val(contents);
                        },
                        onImageUpload: function(files) {
                            var obj = this;
                            var data = new FormData();
                            data.append("file", files[0]);
                            $.ajax({
                                type: "post",
                                url: ctx + "common/upload",
                                data: data,
                                cache: false,
                                contentType: false,
                                processData: false,
                                dataType: 'json',
                                success: function(result) {
                                    if (result.code == web_status.SUCCESS) {
                                        $('#' + obj.id).summernote('insertImage', result.url);
                                    } else {
                                        $.modal.alertError(result.msg);
                                    }
                                },
                                error: function(error) {
                                    $.modal.alertWarning("图片上传失败。");
                                }
                            });
                        }
                    }
                });
                var content = $("input[name='" + this.id + "']").val();
                $('#' + this.id).summernote('code', content);
            });
        });
    </script>
</body>
</html>